﻿using System.Collections.Generic;
using System.Windows.Forms;
using RoiControllers;
using System.Drawing;
using System.ComponentModel;
using System.Linq;

namespace UserControls
{
    public partial class UImageWindow : UImage
    {
        #region 字段|属性

        #region 字段
        private int nextId = 0;                       //设置索引ID
        private ShapeOperateMode drawModel;           //形状操作模式
        private ShapeBase _currentShape;              //当前形状
        private List<ShapeBase> shapeFigureList;      //形状图形列表
        private bool _isDraging = false;              //是否正在拖拽
        private bool _isDrawing = false;              //是否正在绘制
        private PointF drawingPoint = PointF.Empty;   //当前绘制点
        private PointF[] drawingPoints;               //绘制点
        private int index = 0;                        //
        private ShapeType ShapeType;                  //
        #endregion

        #region 属性
        /// <summary> 形状列表 </summary>
        [Category("UserDefine")]
        [Description("形状图形列表")]
        public List<ShapeBase> ShapeFigureList { get => shapeFigureList; }
        public ShapeOperateMode DrawModel { 
            get => drawModel; 
            set => drawModel = value; 
        }
        #endregion

        #region 事件
        public void AttachShapeToWindow(ShapeBase shape)
        {
            shape.ExceteAttachCallback(shape);
            AddShape(shape);
        }
        public void DetachShapeToWindow(ShapeBase shape)
        {
            
        }
        #endregion
        #endregion

        #region 构造函数
        public UImageWindow()
        {
            DoubleBuffered = true;
            shapeFigureList = new List<ShapeBase>();
        }
        #endregion

        #region 其他方法
        public void AddShape(ShapeBase shape)
        {
            ShapeFigureList.ForEach(obj => obj.IsSelected = false);
            ShapeFigureList.Insert(0, shape);
            shape.Name = $"{nextId}";
            shape.Description = $"{shape.Type}_{nextId}";
            shape.IsSelected = true;
            nextId++;
            _currentShape = shape;
            IsOperateImage = false;
        }
        public void RemoveShape(ShapeBase shape)
        {
            ShapeBase shapeObj = ShapeFigureList.FirstOrDefault(obj => obj == shape);
            if (shapeObj != null)
            {
                ShapeFigureList.Remove(shapeObj);
            }
            shape.ExceteDetachCallback(shape);
        }
        private void Drawing(ShapeType type)
        {
            switch(type)
            {
                case ShapeType.Circle:
                    break;
                case ShapeType.Rectangle:
                    break;
                case ShapeType.Rectangle2:
                    break;
                case ShapeType.Polygon:
                    break;
            }
        }
        public void DrawingShape(ShapeType shape)
        {
            ShapeType = shape;
            DrawModel = ShapeOperateMode.Drawing;
        }
        #endregion

        #region 事件方法重写
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            foreach (ShapeBase shape in ShapeFigureList)
            {
                shape?.DrawShapeWithHandle(e.Graphics);
            }
        }
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left)
            {
                ShapeFigureList.FindAll(obj => obj.IsSelected = false);
                if (DrawModel == ShapeOperateMode.Drawing)
                {
                    drawingPoint = e.Location;
                    switch (ShapeType)
                    {
                        case ShapeType.Rectangle:
                            AddShape(new URectangle((int)drawingPoint.X, (int)drawingPoint.Y, 50, 50));
                            break;
                        case ShapeType.Rectangle2:
                            AddShape(new URectangle2(drawingPoint.X, drawingPoint.Y, 50, 50, 0));
                            break;
                        case ShapeType.Circle:
                            AddShape(new URCircle(drawingPoint, 50));
                            break;
                        default:
                            AddShape(new URectangle((int)drawingPoint.X, (int)drawingPoint.Y, 50, 50));
                            break;
                    }
                    this.Invalidate();
                    return;
                }
                foreach (ShapeBase shape in ShapeFigureList)
                {
                    //是否在矩形区域内
                    if (shape.IsInRegion(e.Location))
                    {
                        IsOperateImage = false;
                        _currentShape = shape;
                        _currentShape.IsSelected = true;
                        shape.ExceteSelectedCallback(shape);
                        index = 0;
                        shape.LastLocation = e.Location;
                        //在背景
                        if (shape.IsInBackground(e.Location))
                            DrawModel = ShapeOperateMode.Move;
                        //在旋转手柄上
                        else if (shape.IsInRotateHandle(e.Location))
                        {
                            shape.IsRotating = true;
                            shape.LastLocation = e.Location;
                            DrawModel = ShapeOperateMode.Rotote;
                        }
                        //在手柄
                        else
                            DrawModel = ShapeOperateMode.Resize;
                        _isDraging = true;
                        shape.HandleType = shape.GetHandleType(e.Location);
                        break;
                    }
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                DrawModel = ShapeOperateMode.None;
            }
            this.Invalidate();
        }
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
           
            DrawModel = ShapeOperateMode.None;
            IsOperateImage = true;
            this.Cursor = Cursors.Default;
            if (_currentShape!=null) _currentShape.IsRotating= false;
            shapeFigureList.ForEach(shape => shape.HandleType = ShapeHandleType.None);
            _isDraging = false;
        }
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            bool isInRegion = false;
            //是否在矩形区域内
            foreach (ShapeBase shape in ShapeFigureList)
            {
                if (shape.IsInRegion(e.Location))
                {
                    this.Cursor = shape.GetSystemHandleType(e.Location);
                    isInRegion = true;
                    break;
                }
            }
            if (!isInRegion) this.Cursor = Cursors.Default;
            switch (DrawModel)
            {
                case ShapeOperateMode.None: 
                    break;
                case ShapeOperateMode.Move:
                    _currentShape.Move(e.Location);
                    break;
                case ShapeOperateMode.Resize:
                    _currentShape.Resize(e.Location);
                    break;
                case ShapeOperateMode.Rotote:
                    _currentShape.Rotate(e.Location);
                    break;
            }
            Invalidate();
        }
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            base.OnMouseWheel(e);

        }
        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
        }
        #endregion
    }
}
